<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/11 0011
 * Time: 16:41
 */

namespace app\api\controller\v1\cleaner;


use app\api\controller\v1\Cleaner;
use app\currency\service\CleanerService;
use app\currency\service\DateService;
use app\currency\service\OrderRoyalty;
use think\Db;

class Profit extends Cleaner
{
    /**
     * 收益记录
     * modal:service\bind
     * range: self\team
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $data = $this->_input([
            'modal' => $this->request->get('modal'),
            'range' => $this->request->get('range'),
            'start_at' => $this->request->get('start_at'),
            'end_at' => $this->request->get('end_at'),
        ]);
        in_array($data['modal'],['all','service','bind']) || $data['modal'] = 'all';
        in_array($data['range'],['all','self','team']) || $data['range'] = 'all';
        $start_at = '';
        $end_at = '';
        if(!empty($data['start_at']) && DateService::checkDateFormat($data['start_at']) !== false)
        {
            $start_at = $data['start_at'] . ' 00:00:00';
        }
        if(!empty($data['end_at']) && DateService::checkDateFormat($data['end_at']) !== false)
        {
            $end_at = $data['end_at'] . ' 23:59:59';
        }
        $where = [['cid','eq',$this->cid]];
        $team_ids = CleanerService::getTeamIds($this->cid,2);
        if($data['range'] == 'all')
        {
            $ids = array_merge($team_ids,[$this->cid]);
        } elseif ($data['range'] == 'self')
        {
            $ids = [$this->cid];
        } else {
            $ids = $team_ids;
        }
        if(count($ids) === 1)
        {
            $where[] = ['ccid','eq',$ids[0]];
        } else {
            $where[] = ['ccid','in',$ids];
        }
        if($data['modal'] !== 'all'){
            $where[] = ['modal','eq',$data['modal']];
        }
        if($start_at){
            $where[] = ['create_at','> time',$start_at];
        }
        if($end_at){
            $where[] = ['create_at','<= time',$end_at];
        }
        $query = $this->_query('CleanOrderRoyalty')
            ->where($where);
//        if($start_at !== '' && $end_at !== ''){
//            $query->whereBetweenTime('create_at',$start_at,$end_at);
//        }
        $list = $query->order('create_at desc')->page(true,false,false,10);
        $this->success('获取收益记录成功！', $list);
//        $group = OrderRoyalty::getRoyaltyListByDay($this->cid, $data['modal'], $data['range'], $start_at, $end_at);
//        if($group)
//        {
//            foreach ($group as &$item) {
//                $item['total_amount'] = Db::name('CleanOrderRoyalty')
//                    ->whereIn('id',$item['ids'])
//                    ->sum('amount');
//                $item['list'] = Db::name('CleanOrderRoyalty')
//                    ->alias('a')
//                    ->leftJoin('cleaner b','a.ccid = b.id')
//                    ->leftJoin('member c','a.mid = c.id')
//                    ->whereIn('a.id',$item['ids'])
//                    ->field('a.*,b.name cleaner_name,b.work_headimg,c.nickname,c.headimg')
//                    ->order('create_at desc')
//                    ->select();
//            }
//        }
//        $this->success('获取收益记录成功！', $group);
    }
    protected function _index_page_filter(array &$data){
        $mids = array_unique(array_column($data, 'mid'));
        $memberList = Db::name('Member')->whereIn('id', $mids)->select();
        $cids = array_unique(array_column($data,'cid'));
        $cleanerList = Db::name('Cleaner')->whereIn('id',$cids)->select();
        foreach ($data as &$vo) {
            list($vo['member'], $vo['cleaner']) = [[], []];
            foreach ($memberList as $member) if ($member['id'] === $vo['mid']) {
                $vo['member'] = $member;
            }
            foreach ($cleanerList as $cleaner) if ($cleaner['id'] === $vo['cid']) {
                $vo['cleaner'] = $cleaner;
            }
        }
    }
}